package blazingbubble.game.loader;

import blazingbubble.config.Config;
import moltencore.game.loader.LevelSprite;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import spark.exception.LoadingException;
import spark.graphics.ISpriteMap;
import spark.graphics.SpriteMap;
import spark.graphics.SpriteRenderable;
import spark.math.Vector2;
import spark.math.Vector3;

import java.util.HashMap;
import java.util.LinkedList;

public class LevelParser extends moltencore.game.loader.LevelParser {

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if(localName.equals(moltencore.game.loader.LevelParser.ELEMENT_LEVEL_SPRITE)) {
			LevelSprite sprite = new LevelSprite(new SpriteRenderable(),
			                                     attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_SPRITE_MAP),
			                                     attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_THEME),
			                                     attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_ID));
			try {
				sprite.getRenderable().setPosition(new Vector3(
						Float.parseFloat(attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_POS_X)),
						Float.parseFloat(attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_POS_Y)),
						Float.parseFloat(attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_POS_Z))));
				sprite.getRenderable().setDimensions(new Vector2(
						Float.parseFloat(attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_SCALE_X)),
						Float.parseFloat(attributes.getValue(moltencore.game.loader.LevelParser.ATTRIBUTE_SCALE_Y))));
			}
			catch(NumberFormatException e) {
				throw new SAXException(e);
			}

			level.addSprite(sprite);
		}
	}
}